<?php

class Sfs_Installer extends Zend_Application_Bootstrap_Bootstrap
{
    //    protected function _initAutoload()
    //    {
    //        require_once TOMATO_APP_DIR.'/core/Autoloader.php';
    //        $autoloader = Zend_Loader_Autoloader::getInstance();
    //        $autoloader->unshiftAutoloader(new Tomato_Core_Autoloader(), 'Tomato');
    //        return $autoloader;
    //    }

    protected function _initErrorReporting()
    {
        error_reporting(E_ALL & ~E_NOTICE);
        //date_default_timezone_set('Asia/Taipei');
    }

    protected function _initInstallChecker()
    {
        /**
         * Check whether the app is installed or not
         */
        $config = Sfs_Core_Config::getConfig();


        if ($config->production->install && $config->production->install->date) {

            $request = new Zend_Controller_Request_Http();
            $siteUrl = $request->getScheme().'://'.$request->getHttpHost();
            $basePath = $request->getBasePath();
            $siteUrl = ($basePath == '') ? $siteUrl : $siteUrl.'/'.ltrim($basePath, '/');

            header('Location: '. $siteUrl);
            exit;
        }

    }
    //
    protected function _initRoutes()
    {
        $this->bootstrap('FrontController');
        $front = $this->getResource('FrontController');

        /**
         * Load routes
         */
        $router = $front->getRouter();

        // Don't use default route
        $router->removeDefaultRoutes();
        // Add installer routes

        $router->addRoute(
            'install',
        new Zend_Controller_Router_Route('/',
        array(
                    'module' => 'default',
                    'controller' => 'Install',
                    'action' => 'step1',
        ))
        );

        $router->addRoute(
            'install_step2',
        new Zend_Controller_Router_Route('/step2/',
        array(
                    'module' => 'default',
                    'controller' => 'Install',
                    'action' => 'step2',
        ))
        );
        $router->addRoute(
            'install_step3',
        new Zend_Controller_Router_Route('/step3/',
        array(
                    'module' => 'default',
                    'controller' => 'Install',
                    'action' => 'step3',
        ))
        );

    }


    protected function _initTranslate()
    {
        $registry = Zend_Registry::getInstance();
        /**
         * Set application wide source Locale
         * This is usually your source string language;
         * i.e. $this->translate('Hi I am an English String');
         */
        // TODO 多國語系選擇
        $locale = new Zend_Locale('zh_TW');

        /**
         * Set up and load the translations (all of them!)
         * resources.translate.options.disableNotices = true
         * resources.translate.options.logUntranslated = true
         */
        $translate = new Zend_Translate('array',
        APPLICATION_PATH . DIRECTORY_SEPARATOR .
        'modules'.DIRECTORY_SEPARATOR.
        'default'.DIRECTORY_SEPARATOR
        .'languages', 'auto',
        array(
            'disableNotices' => true,    // This is a very good idea!
            'logUntranslated' => false,  // Change this if you debug
        )
        );
        /**
         * Both of these registry keys are magical and makes
         * ZF 1.7+ do automagical things.
         */
        $registry->set('Zend_Locale', $locale);
        $registry->set('Zend_Translate', $translate);
        return $registry;
    }

    protected function _initView()
    {
        /**
         * Init view
         */
        $config = Sfs_Core_Config::getConfig();

        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        if (null === $viewRenderer->view) {
            $viewRenderer->initView();
        }
        $view = $viewRenderer->view;
        $view->addHelperPath('Sfs/View/Helper', 'Sfs_View_Helper');
        $view->doctype('XHTML1_STRICT');
        $view->headTitle()->setSeparator(' - ');
        $view->headTitle()->prepend('SFS '.$view->translate('Install Wizard'));

        /**
         * Build root URL
         */
        $request = new Zend_Controller_Request_Http();
        $siteUrl = $request->getScheme().'://'.$request->getHttpHost();
        $basePath = $request->getBasePath();
        $siteUrl = ($basePath == '') ? $siteUrl : $siteUrl.'/'.ltrim($basePath, '/');
        $view->assign('APP_URL', $siteUrl);

        /**
         * Set layout path and default layout
         */
        Zend_Layout::startMvc(array('layoutPath' => APPLICATION_PATH.DIRECTORY_SEPARATOR.'layouts'.DIRECTORY_SEPARATOR.'scripts'));
        Zend_Layout::getMvcInstance()->setLayout('install');

    }

    protected function _initPlugins()
    {
        $this->bootstrap('FrontController');
        $front = $this->getResource('FrontController');

        /**
         * Error handler
         */
        $front->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(array(
                                           'module'     => 'default',
                                            'controller' => 'install',
                                            'action'     => 'error',
        )));

    }

    protected function _initDefaultModuleAutoloader()
    {

        $this->bootstrap('frontController');
        $front = $this->frontController;
        $autoloader = Zend_Loader_Autoloader::getInstance();

        $modules = $front->getControllerDirectory();

        $default = $front->getDefaultModule();
        foreach (array_keys($modules) as $module) {
            $autoloader->pushAutoloader(new Zend_Application_Module_Autoloader(array(
                'namespace' => ucwords($module),
                'basePath' => $front->getModuleDirectory($module),
            )));
        }

    }


    protected function _initZFDebug()
    {
        //        $autoloader = Zend_Loader_Autoloader::getInstance();
        //        $autoloader->registerNamespace('ZFDebug');
        //
        //        $options = array(
        //            'plugins' => array(
        //                'Variables',
        //                'File' => array('base_path' => realpath(APPLICATION_PATH . '/../')),
        //                'Memory',
        //                'Time',
        //                'Registry',
        //                'Exception',
        //        ));
        //
        //        # Instantiate the database adapter and setup the plugin.
        //        # Alternatively just add the plugin like above and rely on the autodiscovery feature.
        //        if ($this->hasPluginResource('db')) {
        //            $this->bootstrap('db');
        //            $db = $this->getPluginResource('db')->getDbAdapter();
        //            $options['plugins']['Database']['adapter'] = $db;
        //        }
        //
        //        # Setup the cache plugin
        //        if ($this->hasPluginResource('cache')) {
        //            $this->bootstrap('cache');
        //            $cache = $this-getPluginResource('cache')->getDbAdapter();
        //            $options['plugins']['Cache']['backend'] = $cache->getBackend();
        //        }
        //
        //        $debug = new ZFDebug_Controller_Plugin_Debug($options);
        //
        //        $this->bootstrap('frontController');
        //        $frontController = $this->getResource('frontController');
        //        $frontController->registerPlugin($debug);
    }
}
